package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.NoteTag;
import cn.com.connext.scrm.repository.master.NoteTagRepository;
import cn.com.connext.scrm.service.dto.NoteTagDTO;
import cn.com.connext.scrm.service.mapper.NoteTagMapper;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.vm.NoteTagVM;
import cn.com.connext.scrm.web.rest.vm.PageQueryVM;
import cn.com.connext.scrm.web.rest.vm.QueryIdVM;
import com.connext.common.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: organization-service
 * @author: shawn
 * @create: 2019-06-06 13:51
 **/
@Service
@Transactional
public class NoteTagService extends BaseService<NoteTag, String>{

    @Autowired
    private NoteTagRepository noteTagRepository;

    @Autowired
    private NoteTagMapper noteTagMapper;

    protected NoteTagService(NoteTagRepository repository) {
        super(repository);
        this.noteTagRepository = repository;
    }


    public NoteTagDTO edit(NoteTagVM vm) {
        NoteTag noteTag;
        if (StringUtils.isNotEmpty(vm.getId())){
            noteTag = noteTagRepository.findOne(vm.getId());
        }else {
            noteTag = new NoteTag();
            noteTag.setEnableFlag(false);

        }
        if (StringUtils.isNotEmpty(vm.getCategory())){
            noteTag.setCategory(vm.getCategory());
        }
        if (StringUtils.isNotEmpty(vm.getCode())){
            noteTag.setCode(vm.getCode());
        }
        if (StringUtils.isNotEmpty(vm.getImage_url())){
            noteTag.setImageUrl(vm.getImage_url());
        }
        if (StringUtils.isNotEmpty(vm.getText())){
            noteTag.setText(vm.getText());
        }
        if (vm.getSort() != null){
            noteTag.setSort(vm.getSort());
        }
        if (StringUtils.isNotEmpty(vm.getIcon_url())){
            noteTag.setIconUrl(vm.getIcon_url());
        }
        NoteTag result = noteTagRepository.save(noteTag);
        return noteTagMapper.toDto(result);
    }

    public void deleteById(String id) {
        NoteTag tag = noteTagRepository.findOne(id);
        if (tag == null){
            throw new ServiceException(MyErrorCode.ERROR_DATA_ISNULL);
        }
        noteTagRepository.delete(id);
    }

    public void enableById(QueryIdVM vm) {
        NoteTag tag = noteTagRepository.findOne(vm.getId());
        if (tag == null){
            throw new ServiceException(MyErrorCode.ERROR_DATA_ISNULL);
        }
        if (tag.getEnableFlag()){
            tag.setEnableFlag(false);
        }else {
            tag.setEnableFlag(true);
        }
        noteTagRepository.save(tag);
    }

    public Page<NoteTag> pageQuery(PageQueryVM vm) {
        Page<NoteTag> page = noteTagRepository.findAll(new PageRequest(vm.getPage(), vm.getPageSize(), Sort.Direction.ASC, "sort"));
        return page;
    }

    public NoteTagDTO findById(QueryIdVM vm) {
        NoteTag tag = noteTagRepository.findOne(vm.getId());
        if (tag == null){
            throw new ServiceException(MyErrorCode.ERROR_DATA_ISNULL);
        }
        NoteTagDTO noteTagDTO = noteTagMapper.toDto(tag);
        return noteTagDTO;
    }

    public List<NoteTagDTO> findCategoryList() {
        List<NoteTag> list = noteTagRepository.findCategoryList();
        List<NoteTagDTO> dtoList = noteTagMapper.toDto(list);
        return dtoList;
    }
}
